#include <iostream>
#include "pthread.hpp"
#include "ringqueue.hpp"

using namespace mypthread;
using namespace std;

using RingQueue_t = RingQueue<int>;
void producer(RingQueue_t& rq)
{
    int cnt = 10;
    while(true)
    {
        //sleep(1);
        rq.push(cnt);
        cout << "producer is producting: " << cnt << endl;
        cnt++;
    }

}

void consumer(RingQueue_t& rq)
{
    while(true)
    {
        //sleep(1);
        int data;
        rq.pop(data);
        cout << "consumer is consuming: " << data << endl;
    }
}

void createComThread(vector<Thread<RingQueue_t>> &threads, int num, RingQueue_t& rq, func_t<RingQueue_t> func)
{
    for(int i = 0; i < num; i++)
    {
        string name = "thread-" + to_string(i+1);
        threads.emplace_back(func, rq, name);
        sleep(1);
    }
}

void createProducer(vector<Thread<RingQueue_t>> &threads, int num, RingQueue_t& rq)
{
    createComThread(threads, num, rq, producer);
}

void createConsumer(vector<Thread<RingQueue_t>> &threads, int num, RingQueue_t& rq)
{
    createComThread(threads, num, rq, consumer);
}

void waitAllThread(vector<Thread<RingQueue_t>> &threads)
{
    for (auto &thread : threads)
        thread.join();
}

int main()
{
    RingQueue_t rq(5);
    vector<Thread<RingQueue_t>> threads;

    // 创建一批生产者
    createProducer(threads, 3, rq);

    // 创建一批消费者
    createConsumer(threads, 5, rq);

    // 等待所有线程
    waitAllThread(threads);
}